<template>
  <div class="container" >
    <div class="home-left "  ref="homeLeft">
      <ListView />
    </div>
    <div class="home-right" ref="homeRight">
      <div class="right-title">
        <div class="right-title-text">
          <span>热门推荐</span>
        </div>
      </div>
      <div class="right-content">
        <div class="right-content-item " v-for="item in categoryList" @click="handleClick(item.id)">
          <!-- 图片如果返回接口有图片，则显示图片，否则显示默认图片 -->
          <img src="../assets/images/yzfc.png" />
          <div class="right-content-item-text">{{ item.title }}</div>
        </div>
        <div class="right-more">
          <router-link to="/category">
            <MoreIcon />
            查看更多
          </router-link>
        </div>  
      </div>
      
    </div>
  </div>
</template>

<script setup lang="ts">
import ListView from "@/components/ListView.vue";
import MoreIcon from "@/components/icons/MoreIcon.vue";
import { useRouter } from 'vue-router';
import type { CategoryItem } from '@/service/category/type';
import { ref ,onMounted} from 'vue';
import { useCategoryStore } from '@/stores/categoryStore';
import { gsap } from 'gsap';
const homeLeft = ref(null);
const homeRight = ref(null);


const categoryStore = useCategoryStore();
const categoryList = ref<CategoryItem[]>([]);
const router = useRouter();
const handleClick = (item: number) => {
  router.push(`/category/${item}`);
}
const baseUrl = import.meta.env.VITE_API_BASE_URL;
const getImageUrl = (item: CategoryItem) => {
  return item?.images[0]?.image.filepath ? `${baseUrl}/${item.images[0].image.filepath}` : '../assets/images/yzfc.png';
}
onMounted(async () => {
  // 随机获取分类
  const random = Math.floor(Math.random() * categoryStore.totalPages) + 1;
  await categoryStore.getCategoryList(random,6);
  categoryList.value = categoryStore.categoryList;


  // 使用 GSAP 为 home-left 和 home-right 添加动画
  gsap.from(homeLeft.value, {
    duration: 1,
    x: '100%',
    filter: 'blur(2px)',
    ease: 'power2.out'
  });

  gsap.from(homeRight.value, {
    duration: 1,
    x: '-100%',
    filter: 'blur(2px)',
    ease: 'power2.out'
  });
})


</script>

<style scoped lang="less">
.container {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: space-between;
  .home-left {
    width: 720px;
  }
  .home-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    align-items: center;
    justify-content: left;
    padding: 10px;
    .right-title {
      width: 100%;
    display: flex;
    align-items: center;
    justify-content: left;
      font-weight: bold;
     padding: 5px 0 20px 0;
     color: var(--color);
    }
    .right-content {
      background-color: #fff;
      border-radius: 10px;
      width: 100%;
      display: flex;
      flex-direction: wrap;
      justify-content: space-between;
      padding: 10px;
      flex-wrap: wrap;
      height: 344px;
      .right-more {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 10px 0;
        a {
          color: var(--color);
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
      .right-content-item {
        flex-basis: 33%;
        margin-bottom: 10px;
        img {
          width: 80px;
          height: 80px;
          display: block;
        }
        .right-content-item-text {
          padding-left: 10px;
          font-size: 14px;
          font-weight: bold;
          color: var(--color);
        }
      }
      .right-content-item:hover {
        cursor: pointer;
      }
    }
  }
}

@media (max-width: 768px) {
  .home-left {
    width: 100%;
  }
  .home-right {
    display: none;
  }
}

</style>
